
public class MaxGap {
    public static void main(String[] args) {
        MaxGap m = new MaxGap();
        System.out.println(m.findMaxGap(new int[]{2,7,3,1,1},5));
    }
    public int findMaxGap(int[] A, int n) {
        // write code here
        int max = Integer.MIN_VALUE;
        for(int i = 0; i < n; i++) {
            if(A[i] > max) max = A[i];
        }
        int ans1 = max - A[0];
        int ans2 = max - A[n - 1];
        if(ans1 > ans2) return ans1;
        else return ans2;
    }
}